package com.joysuch.wwyt.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xsx
 * @version 1.0
 * @date 2022/9/6 11:40
 */
public class DateUtils {


    /**
     * 考试用时（毫秒数）转化为xx分xx秒 格式
     */
    public static String getFormatCostTime(Long millisecond){
        StringBuilder costTime = new StringBuilder();
        long minute = millisecond / (1000 * 60);
        long second = (millisecond % (1000 * 60)) / 1000;
        long remainder = (millisecond % (1000 * 60)) % 1000;
        if (remainder>0){
            second++;
        }
        if (minute > 0){
            costTime.append(minute).append("分");
        }
        if (second > 0){
            costTime.append(second).append("秒");
        }
        return costTime.toString();
    }

    public static String getStartOfDay(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    public static String getDate(Date time) {
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    //获取当前周的第一天
    public static Date getFirstDayOfWeek() {
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        int no = calendar.get(Calendar.DAY_OF_WEEK);
        if(no==1){
            calendar.setTime(new Date(calendar.getTimeInMillis()-86400000));
        }
        calendar.set(Calendar.DAY_OF_WEEK,2);
        return calendar.getTime();
    }
    //获取当前周的最后一天
    public static Date getLastDayOfWeek() {
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        int no = calendar.get(Calendar.DAY_OF_WEEK);
        if(no!=1){
            calendar.set(Calendar.DAY_OF_WEEK,7);
            calendar.add(Calendar.DATE,1);
        }
        return calendar.getTime();
    }
    //获取本月的第一天
    public static Date getFirstDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        return calendar.getTime();
    }
    //获取本月的最后一天
    public static Date getLastDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    //获取当天
    public static Date getToDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    //获取昨天
    public static Date getYesterday(){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE,-1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    public static Date getFirstDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }
    public static Date getLastDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    //获取今天之后或之前day天的0点0分0秒
    public static Date getDay(Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    //获取今天之后或之前day天的0点0分0秒
    public static Date getDate(Date date,Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 获取今天之后或之前day天的0点0分0秒
    public static Date getDate(LocalDate localDate, Integer day) {
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return getDate(Date.from(zonedDateTime.toInstant()), day);
    }


    public static Map<String,Date> getTime(Integer way) {
        Map<String,Date> map = new HashMap();
        Date startTime ;
        Date endTime ;
        switch (way) {
            case 2:
                startTime = getFirstDayOfWeek();
                endTime = getLastDayOfWeek();
                break;
            case 3:
                startTime = getFirstDayOfMonth();
                endTime = getLastDayOfMonth();
                break;
                case 4:
                startTime = getFirstDayOfLastMonth();
                endTime = getLastDayOfLastMonth();
                break;
            default:
                startTime = getToDay();
                endTime = startTime;
        }
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        return map;
    }

    //获取当前上一时段的59分59秒
    public static Date getDayUpperHour(Date time) {
        if (time==null){
            time=new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long l = calendar.getTime().getTime() - 1000L;
        return new Date(l);
    }

    //获取当前下一时段的0分0秒
    public static Date getDayHour(Date time) {
        if (time==null){
            time=new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long l = calendar.getTime().getTime() + 3600000L;
        return new Date(l);
    }
    //获取当前时刻
    public static Date getDayNowHour(Date time) {
        if (time==null){
            time=new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }




    public static Date getDay(Date date,Integer day) {
        if (date==null){
            date=new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getDayNowHour(null));
    }
}
